package spring.cloud.service.task.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import spring.cloud.service.task.service.ZooKeeperService;
import spring.cloud.service.task.utils.PageInfo;
import spring.cloud.service.task.utils.Result;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RequestMapping("/zookeeper")
@RestController
public class ZooKeeperController {
    @Autowired
    private ZooKeeperService zooKeeperService;

    /**
     * page: 1
     * limit: 10
     */
    @GetMapping(value = "/getZooKeeperList")
    public Result getZooKeeperList(@RequestParam(required = false) String description, @RequestParam(required = false) String status, //
                                   @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit) throws RuntimeException {
        Result result = new Result();
        result.setCode(Result.table_query_success_code);
        result.setMsg(Result.table_query_success_msg);
        status = StringUtils.trimToNull(status);//下拉框""为空BUG fix
        PageInfo pageInfo = zooKeeperService.getZooKeeperList(description, status, page, limit);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", pageInfo.getTotalCount());
        map.put("records", pageInfo.getRecords());
        result.setData(map);
        return result;
    }

    @PostMapping(value = "/addZooKeeper")
    public Result addZooKeeper(@RequestBody String data) throws RuntimeException {
        JSONObject jsonObject = JSON.parseObject(data);
        String zooKeeperKey = UUID.randomUUID().toString();
        String description = jsonObject.getString("description");
        String addressList = jsonObject.getString("addressList");
        String digest = jsonObject.getString("digest");
        String status = jsonObject.getString("status");
        Result result = zooKeeperService.addZooKeeper(zooKeeperKey, description, addressList, digest, status);
        return result;
    }

    @PostMapping(value = "/updateZooKeeper")
    public Result updateZooKeeper(@RequestBody String data) throws RuntimeException {
        JSONObject jsonObject = JSON.parseObject(data);
        String zooKeeperKey = jsonObject.getString("zooKeeperKey");
        String description = jsonObject.getString("description");
        String addressList = jsonObject.getString("addressList");
        String digest = jsonObject.getString("digest");
        String status = jsonObject.getString("status");
        Result result = zooKeeperService.updateZooKeeper(zooKeeperKey, description, addressList, digest, status);
        return result;
    }

    @PostMapping(value = "/updateZooKeeperStatus")
    public Result updateZooKeeperStatus(@RequestBody String data) throws RuntimeException {
        JSONObject jsonObject = JSON.parseObject(data);
        String zooKeeperKey = jsonObject.getString("zooKeeperKey");
        String status = jsonObject.getString("status");
        Result result = zooKeeperService.updateZooKeeperStatus(zooKeeperKey, status);
        return result;
    }

    @PostMapping(value = "/deleteZooKeeper")
    public Result deleteZooKeeper(@RequestParam(name = "zooKeeperKey") String zooKeeperKey) throws RuntimeException {
        Result result = zooKeeperService.deleteZooKeeper(zooKeeperKey);
        return result;
    }


    /**
     * ui公共方法
     *
     * @return
     * @throws RuntimeException
     */
    @GetMapping(value = "/getZooKeeperKeyAndDescriptionList")
    public Result getZooKeeperKeyAndDescriptionList() throws RuntimeException {
        Result result = Result.success();
        List<HashMap<String, String>> list = zooKeeperService.getZooKeeperKeyAndDescriptionList();
        result.setData(list);
        return result;
    }
}
